from fastapi import APIRouter, Body
from backend.core.response import success_response, error_response
import sys
from backend.config import is_dev_mode
from backend.logger import log  # 导入日志组件

router = APIRouter()
@router.get("/hello")
async def hello():
    log.info("处理 /hello 请求")
    return success_response(data={"message": "Hello from FastAPI!"}).to_dict()

@router.post("/calculate")
async def calculate(a: int = Body(...), b: int = Body(...)):
    log.debug(f"计算请求: a={a}, b={b}")
    result = a + b
    log.debug(f"计算结果: {result}")
    return success_response(data=result).to_dict()

@router.get("/system-info")
async def system_info():
    log.debug("获取系统信息")
    data = {
        "platform": sys.platform,
        "python_version": sys.version,
        "dev_mode": is_dev_mode()
    }
    return success_response(data=data).to_dict()

# 全局异常处理路由
@router.get("/error-test")
async def error_test():
    try:
        # 模拟错误
        1 / 0
    except Exception as e:
        return error_response(message="测试错误", code=500, data=str(e)).to_dict()